package com.exporter.utility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.exporter.exception.ExporterException;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class ExporterUtility
{

    public static List< String > modifiedFilesList = new ArrayList< String >();
    public static String todayDate = "";
    public static Set< String > modificationTimes = new HashSet< String >();
    public static Map< String, Integer > modificationDetail = new HashMap< String, Integer >();
    public static String modificationDate = "";
    public static Set< String > allLastModifiedDate = new TreeSet< String >();
    public static Date dateRangeFrom;
    public static Date dateRangeTo;
    public static String specifiedDate;
    public static String theSelectedDate;
    public static String zipfileName;

    public static void getRecursiveListing(String path, String folderIgnore, String fileIgnore, String searchType)
    {
        File directory = new File( path );
        String[] files = directory.list();
        for ( int i = 0; i < files.length; i++ )
        {
            File procFile = new File( directory, files[i] );

            if ( procFile.isDirectory() && !folderIgnore.contains( procFile.getName() ) )
            {
                getRecursiveListing( procFile.getPath(), folderIgnore, fileIgnore, searchType );
            }
            else if ( procFile.isFile() && procFile.getName().lastIndexOf( "." ) != -1 )
            {
                String extn = procFile.getName().substring( procFile.getName().lastIndexOf( "." ) );
                SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy_HH.mm" );
                String today = sdf.format( new Date() );
                todayDate = today.split( "_" )[0];
                String importDate = "";
                if ( searchType.equals( "searchOnTime" ) )
                {
                    importDate = modificationDate.split( "_" )[0];
                    String importTime = modificationDate.split( "_" )[1];
                    String lastModifiedDate = sdf.format( new Date( procFile.lastModified() ) ).split( "_" )[0];
                    String lastModifiedTime = sdf.format( new Date( procFile.lastModified() ) ).split( "_" )[1];

                    Calendar importCal = Calendar.getInstance();
                    importCal.set( Calendar.HOUR_OF_DAY, Integer.parseInt( importTime.split( "\\." )[0] ) );
                    importCal.set( Calendar.MINUTE, Integer.parseInt( importTime.split( "\\." )[1] ) );

                    Calendar lastModifyCal = Calendar.getInstance();
                    lastModifyCal.set( Calendar.HOUR_OF_DAY, Integer.parseInt( lastModifiedTime.split( "\\." )[0] ) );
                    lastModifyCal.set( Calendar.MINUTE, Integer.parseInt( lastModifiedTime.split( "\\." )[1] ) );

                    if ( !fileIgnore.contains( extn ) && lastModifiedDate.equals( importDate ) && lastModifyCal.after( importCal ) )
                    {
                        modifiedFilesList.add( procFile.getPath() );
                    }
                }
                else if ( searchType.equals( "searchOnDate" ) )
                {
                    importDate = modificationDate;
                    String lastModifiedDate = sdf.format( new Date( procFile.lastModified() ) ).split( "_" )[0];
                    if ( !fileIgnore.contains( extn ) && !lastModifiedDate.equals( importDate ) && lastModifiedDate.equals( todayDate ) )
                    {
                        modifiedFilesList.add( procFile.getPath() );
                    }
                }
            }
        }
    }

    public static boolean copyModifiedFiles(String srcFolder, String destFolder, boolean folderStructure, boolean logFile) throws ExporterException
    {
        String todayChangedFiles = destFolder + "\\" + todayDate;
        File changedFilesFolder = new File( todayChangedFiles );
        boolean flag = false;
        if ( !changedFilesFolder.exists() )
        {
            changedFilesFolder.mkdir();
        }
        else
        {
            removeDirectory( changedFilesFolder );
            changedFilesFolder.mkdir();
        }
        if ( folderStructure )
        {
            for ( int i = 0; i < modifiedFilesList.size(); i++ )
            {
                String filePath = modifiedFilesList.get( i );
                filePath = filePath.replace( srcFolder, todayChangedFiles );
                String dirPath = filePath.substring( 0, filePath.lastIndexOf( "\\" ) );
                File dirStructure = new File( dirPath );
                if ( !dirStructure.exists() )
                {
                    dirStructure.mkdirs();
                }
                copyFile( modifiedFilesList.get( i ), filePath );
            }
        }
        else
        {
            for ( int i = 0; i < modifiedFilesList.size(); i++ )
            {
                String fileName = modifiedFilesList.get( i ).substring( modifiedFilesList.get( i ).lastIndexOf( "\\" ) + 1 );
                String destFile = todayChangedFiles + "\\" + fileName;
                copyFile( modifiedFilesList.get( i ), destFile );
            }
        }
        if ( logFile && modifiedFilesList.size() > 0 )
        {
            File file = new File( destFolder + "\\" + todayDate + "\\changeLog" );
            file.mkdir();
            file = new File( destFolder + "\\" + todayDate + "\\changeLog\\changeLog_" + todayDate + ".log" );
            String content = "";
            SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy HH:mm:ss :: " );
            for ( int i = 0; i < modifiedFilesList.size(); i++ )
            {
                File tempFile = new File( modifiedFilesList.get( i ) );
                content = content + sdf.format( tempFile.lastModified() ) + modifiedFilesList.get( i ) + "\r\n";
            }
            try
            {
                FileOutputStream fos = new FileOutputStream( file );
                byte[] fileContent = content.getBytes();
                fos.write( fileContent );
                fos.close();
            }
            catch ( FileNotFoundException e )
            {
                throw new ExporterException( "Cannot create log file" );
            }
            catch ( IOException e )
            {
                throw new ExporterException( "Error occured while creating log file" );
            }
            finally
            {
                modifiedFilesList.clear();
                flag = true;
            }

        }
        else if(!logFile)
        {
        	flag = true;
        }
        else
        {
            flag = false;
        }
        return flag;
    }

    private static boolean removeDirectory(File directory)
    {
        if ( directory == null )
            return false;
        if ( !directory.exists() )
            return true;
        if ( !directory.isDirectory() )
            return false;

        String[] list = directory.list();
        if ( list != null )
        {
            for ( int i = 0; i < list.length; i++ )
            {
                File entry = new File( directory, list[i] );
                if ( entry.isDirectory() )
                {
                    if ( !removeDirectory( entry ) )
                        return false;
                }
                else
                {
                    if ( !entry.delete() )
                        return false;
                }
            }
        }

        return directory.delete();
    }

    private static void copyFile(String inFile, String outFile) throws ExporterException
    {
        File in = new File( inFile );
        File out = new File( outFile );

        try
        {
            FileInputStream fis = new FileInputStream( in );
            byte[] buffer = new byte[fis.available()];
            fis.read( buffer );

            FileOutputStream fos = new FileOutputStream( out );
            fos.write( buffer );

            fis.close();
            fos.close();
        }
        catch ( FileNotFoundException e )
        {
            throw new ExporterException( "Requested file not found" );
        }
        catch ( IOException e )
        {
            throw new ExporterException( "Error occured while copying the files" );
        }
    }

    public static boolean createZipFile(String destFolder) throws ExporterException
    {
        boolean flag = false;
        String fileName = todayDate;
        File directory = new File( destFolder );
        if ( !"".equals( todayDate ) && new File( destFolder + "\\" + todayDate ).exists() )
        {
            directory = new File( destFolder + "\\" + todayDate );
        }
        else if ( new File( destFolder + "\\ChangedFiles" ).exists() )
        {
            directory = new File( destFolder + "\\ChangedFiles" );
            fileName = "ChangedFiles";
        }
        else if ( !"".equals( theSelectedDate ) && new File( destFolder + "\\" + theSelectedDate ).exists() )
        {
            directory = new File( destFolder + "\\" + theSelectedDate );
            fileName = theSelectedDate;
        }

        if ( directory.list().length > 0 )
        {
            File zipFile = new File( destFolder + "\\" + fileName + ".zip" );
            try
            {
                ZipOutputStream zos = new ZipOutputStream( new FileOutputStream( zipFile ) );
                zip( directory, directory, zos );
                zos.close();
            }
            catch ( FileNotFoundException e )
            {
                throw new ExporterException( "The requested zip file was not found" );
            }
            catch ( IOException e )
            {
                throw new ExporterException( "Error occured while creating the zip file" );
            }
            finally
            {
                flag = true;
            }
        }
        zipfileName = fileName;
        return flag;
    }

    private static final void zip(File directory, File base, ZipOutputStream zos) throws IOException
    {
        File[] files = directory.listFiles();
        byte[] buffer = new byte[8192];
        int read = 0;
        for ( int i = 0, n = files.length; i < n; i++ )
        {
            if ( files[i].isDirectory() )
            {
                zip( files[i], base, zos );
            }
            else
            {
                FileInputStream in = new FileInputStream( files[i] );
                ZipEntry entry = new ZipEntry( files[i].getPath().substring( base.getPath().length() + 1 ) );
                zos.putNextEntry( entry );
                while ( -1 != (read = in.read( buffer )) )
                {
                    zos.write( buffer, 0, read );
                }
                in.close();
            }
        }
    }

    public static boolean uploadToSftp(List< String > configParams) throws ExporterException
    {
        boolean flag = false;
        String host = configParams.get( 0 );
        int port;
        if ( configParams.get( 1 ).contains( "," ) )
        {
            String[] ports = configParams.get( 1 ).split( "," );
            String portNumber = "";
            for ( int i = 0; i < ports.length; i++ )
            {
                portNumber = portNumber + ports[i];
            }
            port = Integer.parseInt( portNumber );
        }
        else
        {
            port = Integer.parseInt( configParams.get( 1 ) );
        }
        String user = configParams.get( 2 );
        String pass = configParams.get( 3 );
        String sftpDir = configParams.get( 4 );
        String fileToUpload = configParams.get( 5 ) + "\\" + zipfileName + ".zip";
        File file = new File( fileToUpload );
        if ( file.exists() )
        {
            Session session = null;
            Channel channel = null;
            ChannelSftp channelSftp = null;
            try
            {
                JSch jsch = new JSch();
                session = jsch.getSession( user, host, port );
                session.setPassword( pass );
                java.util.Properties config = new java.util.Properties();
                config.put( "StrictHostKeyChecking", "no" );
                session.setConfig( config );
                session.connect();
                channel = session.openChannel( "sftp" );
                channel.connect();
                channelSftp = (ChannelSftp) channel;
                channelSftp.cd( sftpDir );
                channelSftp.put( new FileInputStream( file ), file.getName() );
            }
            catch ( Exception ex )
            {
                throw new ExporterException( "Error occured while uploading to SFTP" );
            }
            finally
            {
                flag = true;
            }
        }
        return flag;
    }

    public static boolean analyzeWorkspace(String path, String folderIgnore, String fileIgnore) throws ExporterException
    {
        File workspace = new File( path + "\\.metadata" );
        if ( workspace.exists() && workspace.isDirectory() )
        {
            traverseFiles( path, folderIgnore, fileIgnore );
            int maxEntry = 0;
            Iterator< Entry< String, Integer >> it = modificationDetail.entrySet().iterator();
            while ( it.hasNext() )
            {
                Map.Entry< String, Integer > entry = it.next();
                if ( entry.getValue().intValue() > maxEntry )
                {
                    maxEntry = entry.getValue().intValue();
                    modificationDate = entry.getKey();
                }
            }
            SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy" );
            String today = sdf.format( new Date() );
            if ( today.equals( modificationDate.split( "_" )[0] ) )
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            throw new ExporterException( "Please select a Eclipse/RAD workspace" );
        }
    }

    private static void traverseFiles(String path, String folderIgnore, String fileIgnore)
    {
        File directory = new File( path );
        String[] files = directory.list();
        for ( int i = 0; i < files.length; i++ )
        {
            File procFile = new File( directory, files[i] );

            if ( procFile.isDirectory() && !folderIgnore.contains( procFile.getName() ) )
            {
                traverseFiles( procFile.getPath(), folderIgnore, fileIgnore );
            }
            else if ( procFile.isFile() && procFile.getName().lastIndexOf( "." ) != -1 )
            {
                String extn = procFile.getName().substring( procFile.getName().lastIndexOf( "." ) );
                SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy_HH.mm" );
                String lastModifiedDate = sdf.format( new Date( procFile.lastModified() ) );
                if ( !fileIgnore.contains( extn ) )
                {
                    if ( modificationDetail.get( lastModifiedDate ) == null )
                    {
                        modificationDetail.put( lastModifiedDate, new Integer( 1 ) );
                    }
                    else
                    {
                        int x = modificationDetail.get( lastModifiedDate ).intValue();
                        x++;
                        modificationDetail.put( lastModifiedDate, new Integer( x ) );
                    }
                }
            }
        }
    }

    public static List< String > analyzeAndGenerateDates(String path, String folderIgnore, String fileIgnore)
    {
        traverseFilesForDate( path, folderIgnore, fileIgnore );
        List< String > dateList = new ArrayList< String >();
        Iterator< String > dateIterator = allLastModifiedDate.iterator();
        while ( dateIterator.hasNext() )
        {
            dateList.add( dateIterator.next() );
        }
        Collections.sort( dateList, new DateComparator( "dsc" ) );
        return dateList;
    }

    private static void traverseFilesForDate(String path, String folderIgnore, String fileIgnore)
    {
        File directory = new File( path );
        String[] files = directory.list();
        for ( int i = 0; i < files.length; i++ )
        {
            File procFile = new File( directory, files[i] );

            if ( procFile.isDirectory() && !folderIgnore.contains( procFile.getName() ) )
            {
                traverseFilesForDate( procFile.getPath(), folderIgnore, fileIgnore );
            }
            else if ( procFile.isFile() && procFile.getName().lastIndexOf( "." ) != -1 )
            {
                String extn = procFile.getName().substring( procFile.getName().lastIndexOf( "." ) );
                if ( !fileIgnore.contains( extn ) )
                {
                    SimpleDateFormat sdf = new SimpleDateFormat( "EEE, dd MMM, yyyy" );
                    String lastModifiedDate = sdf.format( new Date( procFile.lastModified() ) );
                    allLastModifiedDate.add( lastModifiedDate );
                }
            }
        }
    }

    public static boolean exportFilesInDateRange(String fromDate, String toDate, String srcFolder, String destFolder, String fileIgnore, String folderIgnore, boolean folderStructure, boolean logFile) throws ExporterException
    {
        SimpleDateFormat sdf = new SimpleDateFormat( "EEE, dd MMM, yyyy" );
        try
        {
            dateRangeFrom = sdf.parse( fromDate );
            dateRangeTo = sdf.parse( toDate );
        }
        catch ( ParseException ex )
        {
            throw new ExporterException( "Error occured while parsing the date" );
        }
        searchFilesInDateRange( srcFolder, fileIgnore, folderIgnore );
        return copyModifiedFilesInRange( srcFolder, destFolder, folderStructure, logFile );
    }

    private static void searchFilesInDateRange(String srcFolder, String fileIgnore, String folderIgnore) throws ExporterException
    {
        File directory = new File( srcFolder );
        String[] files = directory.list();
        for ( int i = 0; i < files.length; i++ )
        {
            File procFile = new File( directory, files[i] );
            String extn = "";
            if ( procFile.getName().lastIndexOf( "." ) != -1 )
            {
                extn = procFile.getName().substring( procFile.getName().lastIndexOf( "." ) );
            }
            if ( procFile.isDirectory() && !folderIgnore.contains( procFile.getName() ) )
            {
                searchFilesInDateRange( procFile.getPath(), fileIgnore, folderIgnore );
            }
            else if ( procFile.isFile() && procFile.getName().lastIndexOf( "." ) != -1 && !fileIgnore.contains( extn ) )
            {
                SimpleDateFormat sdf = new SimpleDateFormat( "EEE, dd MMM, yyyy" );
                try
                {
                    Date currentFile = sdf.parse( sdf.format( new Date( procFile.lastModified() ) ) );
                    if ( currentFile.after( dateRangeFrom ) && currentFile.before( dateRangeTo ) )
                    {
                        modifiedFilesList.add( procFile.getPath() );
                    }
                    else if ( sdf.format( dateRangeFrom ).equals( sdf.format( currentFile.getTime() ) ) || sdf.format( dateRangeTo ).equals( sdf.format( currentFile.getTime() ) ) )
                    {
                        modifiedFilesList.add( procFile.getPath() );
                    }
                }
                catch ( ParseException e )
                {
                    throw new ExporterException( "Error occured while parsing the date" );
                }
            }
        }
    }

    public static boolean validateWorkspace(String path) throws ExporterException
    {
        File workspace = new File( path + "\\.metadata" );
        if ( workspace.exists() && workspace.isDirectory() )
        {
            return true;
        }
        else
        {
            throw new ExporterException( "Please select a Eclipse/RAD workspace" );
        }
    }

    public static boolean exportFilesOfSpecificDate(String specificDate, String srcFolder, String destFolder, String fileIgnore, String folderIgnore, boolean folderStructure, boolean logFile) throws ExporterException
    {
        String theDate = "";
        try
        {
            specifiedDate = specificDate;
            SimpleDateFormat sdfTarget = new SimpleDateFormat( "dd.MM.yyyy" );
            SimpleDateFormat sdfSource = new SimpleDateFormat( "EEE, dd MMM, yyyy" );
            theDate = sdfTarget.format( sdfSource.parse( specificDate ) );
            theSelectedDate = theDate;
        }
        catch ( ParseException e )
        {
            e.printStackTrace();
        }
        searchFilesOfDate( srcFolder, fileIgnore, folderIgnore );
        return copyModifiedFilesSelectedDate( srcFolder, destFolder, folderStructure, theDate, logFile );
    }

    private static void searchFilesOfDate(String srcFolder, String fileIgnore, String folderIgnore)
    {
        File directory = new File( srcFolder );
        String[] files = directory.list();
        for ( int i = 0; i < files.length; i++ )
        {
            File procFile = new File( directory, files[i] );

            if ( procFile.isDirectory() && !folderIgnore.contains( procFile.getName() ) )
            {
                searchFilesOfDate( procFile.getPath(), fileIgnore, folderIgnore );
            }
            else if ( procFile.isFile() )
            {
                SimpleDateFormat sdf = new SimpleDateFormat( "EEE, dd MMM, yyyy" );
                String lastModifiedDate = sdf.format( new Date( procFile.lastModified() ) );
                if ( specifiedDate.equals( lastModifiedDate ) )
                {
                    modifiedFilesList.add( procFile.getPath() );
                }
            }
        }
    }

    public static boolean copyModifiedFilesInRange(String srcFolder, String destFolder, boolean folderStructure, boolean logFile) throws ExporterException
    {
        String todayChangedFiles = destFolder + "\\ChangedFiles";
        File changedFilesFolder = new File( todayChangedFiles );
        boolean flag = false;
        if ( !changedFilesFolder.exists() )
        {
            changedFilesFolder.mkdir();
        }
        else
        {
            removeDirectory( changedFilesFolder );
            changedFilesFolder.mkdir();
        }
        if ( folderStructure )
        {
            for ( int i = 0; i < modifiedFilesList.size(); i++ )
            {
                String filePath = modifiedFilesList.get( i );
                SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy" );
                String changedDate = sdf.format( new File( filePath ).lastModified() );
                String tempChangedFiles = todayChangedFiles + "\\" + changedDate;
                filePath = filePath.replace( srcFolder, tempChangedFiles );
                String dirPath = filePath.substring( 0, filePath.lastIndexOf( "\\" ) );
                File dirStructure = new File( dirPath );
                if ( !dirStructure.exists() )
                {
                    dirStructure.mkdirs();
                }
                copyFile( modifiedFilesList.get( i ), filePath );
            }
        }
        else
        {
            for ( int i = 0; i < modifiedFilesList.size(); i++ )
            {
                String fileName = modifiedFilesList.get( i ).substring( modifiedFilesList.get( i ).lastIndexOf( "\\" ) + 1 );
                String destFile = todayChangedFiles + "\\" + fileName;
                copyFile( modifiedFilesList.get( i ), destFile );
            }
        }
        if ( logFile && modifiedFilesList.size() > 0 )
        {
            File file = new File( destFolder + "\\ChangedFiles\\changeLog" );
            file.mkdir();
            file = new File( destFolder + "\\ChangedFiles\\changeLog\\changeLog_DateRange.log" );
            String content = "";
            SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy HH:mm:ss :: " );
            for ( int i = 0; i < modifiedFilesList.size(); i++ )
            {
                File tempFile = new File( modifiedFilesList.get( i ) );
                content = content + sdf.format( tempFile.lastModified() ) + modifiedFilesList.get( i ) + "\r\n";
            }
            try
            {
                FileOutputStream fos = new FileOutputStream( file );
                byte[] fileContent = content.getBytes();
                fos.write( fileContent );
                fos.close();
            }
            catch ( FileNotFoundException e )
            {
                throw new ExporterException( "Cannot create log file" );
            }
            catch ( IOException e )
            {
                throw new ExporterException( "Error occured while creating log file" );
            }
            finally
            {
                modifiedFilesList.clear();
                flag = true;
            }

        }
        else if(!logFile)
        {
        	flag = true;
        }
        else
        {
            flag = false;
        }
        return flag;
    }

    public static boolean copyModifiedFilesSelectedDate(String srcFolder, String destFolder, boolean folderStructure, String theDate, boolean logFile) throws ExporterException
    {
        String todayChangedFiles = destFolder + "\\" + theSelectedDate;
        File changedFilesFolder = new File( todayChangedFiles );
        boolean flag = false;
        if ( !changedFilesFolder.exists() )
        {
            changedFilesFolder.mkdir();
        }
        else
        {
            removeDirectory( changedFilesFolder );
            changedFilesFolder.mkdir();
        }
        if ( folderStructure )
        {
            for ( int i = 0; i < modifiedFilesList.size(); i++ )
            {
                String filePath = modifiedFilesList.get( i );
                filePath = filePath.replace( srcFolder, todayChangedFiles );
                String dirPath = filePath.substring( 0, filePath.lastIndexOf( "\\" ) );
                File dirStructure = new File( dirPath );
                if ( !dirStructure.exists() )
                {
                    dirStructure.mkdirs();
                }
                copyFile( modifiedFilesList.get( i ), filePath );
            }
        }
        else
        {
            for ( int i = 0; i < modifiedFilesList.size(); i++ )
            {
                String fileName = modifiedFilesList.get( i ).substring( modifiedFilesList.get( i ).lastIndexOf( "\\" ) + 1 );
                String destFile = todayChangedFiles + "\\" + fileName;
                copyFile( modifiedFilesList.get( i ), destFile );
            }
        }
        if ( logFile && modifiedFilesList.size() > 0 )
        {
            File file = new File( destFolder + "\\" + theDate + "\\changeLog" );
            file.mkdir();
            file = new File( destFolder + "\\" + theDate + "\\changeLog\\changeLog_" + theDate + ".log" );
            String content = "";
            SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy HH:mm:ss :: " );
            for ( int i = 0; i < modifiedFilesList.size(); i++ )
            {
                File tempFile = new File( modifiedFilesList.get( i ) );
                content = content + sdf.format( tempFile.lastModified() ) + modifiedFilesList.get( i ) + "\r\n";
            }
            try
            {
                FileOutputStream fos = new FileOutputStream( file );
                byte[] fileContent = content.getBytes();
                fos.write( fileContent );
                fos.close();
            }
            catch ( FileNotFoundException e )
            {
                throw new ExporterException( "Cannot create log file" );
            }
            catch ( IOException e )
            {
                throw new ExporterException( "Error occured while creating log file" );
            }
            finally
            {
                modifiedFilesList.clear();
                flag = true;
            }
        }
        else if(!logFile)
        {
        	flag = true;
        }
        else
        {
            flag = false;
        }
        return flag;

    }

}
